home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2007 December / PCWKCD1207B.iso / Blogowanie poza sfera / Flock 0.9.1.3 stable / flock-0.9.1.3.en-US.win32.exe / flock / chrome / browser.jar / content / browser / preferences / permissions.js < prev    next >
Text File  |  2006-07-18  |  13KB  |  359 lines

  1. //@line 39 "/cygdrive/K/tinderbuild/src/flock/mozilla/browser/components/preferences/permissions.js"
  2.  
  3. const nsIPermissionManager = Components.interfaces.nsIPermissionManager;
  4. const nsICookiePermission = Components.interfaces.nsICookiePermission;
  5.  
  6. function Permission(host, rawHost, type, capability, perm) 
  7. {
  8.   this.host = host;
  9.   this.rawHost = rawHost;
  10.   this.type = type;
  11.   this.capability = capability;
  12.   this.perm = perm;
  13. }
  14.  
  15. var gPermissionManager = {
  16.   _type         : "",
  17.   _permissions  : [],
  18.   _pm           : Components.classes["@mozilla.org/permissionmanager;1"]
  19.                             .getService(Components.interfaces.nsIPermissionManager),
  20.   _bundle       : null,
  21.   _tree         : null,
  22.   
  23.   _view: {
  24.     _rowCount: 0,
  25.     get rowCount() 
  26.     { 
  27.       return this._rowCount; 
  28.     },
  29.     getCellText: function (aRow, aColumn)
  30.     {
  31.       if (aColumn.id == "siteCol")
  32.         return gPermissionManager._permissions[aRow].rawHost;
  33.       else if (aColumn.id == "statusCol")
  34.         return gPermissionManager._permissions[aRow].capability;
  35.       return "";
  36.     },
  37.  
  38.     isSeparator: function(aIndex) { return false; },
  39.     isSorted: function() { return false; },
  40.     isContainer: function(aIndex) { return false; },
  41.     setTree: function(aTree){},
  42.     getImageSrc: function(aRow, aColumn) {},
  43.     getProgressMode: function(aRow, aColumn) {},
  44.     getCellValue: function(aRow, aColumn) {},
  45.     cycleHeader: function(column) {},
  46.     getRowProperties: function(row,prop){},
  47.     getColumnProperties: function(column,prop){},
  48.     getCellProperties: function(row,column,prop){}
  49.   },
  50.   
  51.   _getCapabilityString: function (aCapability)
  52.   {
  53.     var stringKey = null;
  54.     switch (aCapability) {
  55.     case nsIPermissionManager.ALLOW_ACTION:
  56.       stringKey = "can";
  57.       break;
  58.     case nsIPermissionManager.DENY_ACTION:
  59.       stringKey = "cannot";
  60.       break;
  61.     case nsICookiePermission.ACCESS_SESSION:
  62.       stringKey = "canSession";
  63.       break;
  64.     }
  65.     return this._bundle.getString(stringKey);
  66.   },
  67.   
  68.   addPermission: function (aCapability)
  69.   {
  70.     var textbox = document.getElementById("url");
  71.     var host = textbox.value.replace(/^\s*([-\w]*:\/+)?/, ""); // trim any leading space and scheme
  72.     try {
  73.       var ioService = Components.classes["@mozilla.org/network/io-service;1"]
  74.                                 .getService(Components.interfaces.nsIIOService);
  75.       var uri = ioService.newURI("http://"+host, null, null);
  76.       host = uri.host;
  77.     } catch(ex) {
  78.       var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
  79.                                     .getService(Components.interfaces.nsIPromptService);
  80.       var message = this._bundle.getString("invalidURI");
  81.       var title = this._bundle.getString("invalidURITitle");
  82.       promptService.alert(window, title, message);
  83.       return;
  84.     }
  85.  
  86.     var capabilityString = this._getCapabilityString(aCapability);
  87.  
  88.     // check whether the permission already exists, if not, add it
  89.     var exists = false;
  90.     for (var i = 0; i < this._permissions.length; ++i) {
  91.       if (this._permissions[i].rawHost == host) {
  92.         exists = true;
  93.         this._permissions[i].capability = capabilityString;
  94.         this._permissions[i].perm = aCapability;
  95.         break;
  96.       }
  97.     }
  98.     
  99.     if (!exists) {
  100.       host = (host.charAt(0) == ".") ? host.substring(1,host.length) : host;
  101.       var uri = ioService.newURI("http://" + host, null, null);
  102.       this._pm.add(uri, this._type, aCapability);
  103.     }
  104.     textbox.value = "";
  105.     textbox.focus();
  106.  
  107.     // covers a case where the site exists already, so the buttons don't disable
  108.     this.onHostInput(textbox);
  109.  
  110.     // enable "remove all" button as needed
  111.     document.getElementById("removeAllPermissions").disabled = this._permissions.length == 0;
  112.   },
  113.   
  114.   onHostInput: function (aSiteField)
  115.   {
  116.     document.getElementById("btnSession").disabled = !aSiteField.value;
  117.     document.getElementById("btnBlock").disabled = !aSiteField.value;
  118.     document.getElementById("btnAllow").disabled = !aSiteField.value;
  119.   },
  120.   
  121.   onHostKeyPress: function (aEvent)
  122.   {
  123.     if (aEvent.keyCode == 13)
  124.       gPermissionManager.addPermission(nsIPermissionManager.ALLOW_ACTION);
  125.   },
  126.   
  127.   onLoad: function ()
  128.   {
  129.     this._bundle = document.getElementById("bundlePreferences");
  130.     var params = window.arguments[0];
  131.     this.init(params);
  132.   },
  133.   
  134.   init: function (aParams)
  135.   {
  136.     this._type = aParams.permissionType;
  137.     
  138.     var permissionsText = document.getElementById("permissionsText");
  139.     while (permissionsText.hasChildNodes())
  140.       permissionsText.removeChild(permissionsText.firstChild);
  141.     permissionsText.appendChild(document.createTextNode(aParams.introText));
  142.  
  143.     document.title = aParams.windowTitle;
  144.     
  145.     document.getElementById("btnBlock").hidden    = !aParams.blockVisible;
  146.     document.getElementById("btnSession").hidden  = !aParams.sessionVisible;
  147.     document.getElementById("btnAllow").hidden    = !aParams.allowVisible;
  148.     
  149.     var urlField = document.getElementById("url");
  150.     urlField.value = aParams.prefilledHost;
  151.     
  152.     this.onHostInput(urlField);
  153.     
  154.     var os = Components.classes["@mozilla.org/observer-service;1"]
  155.                        .getService(Components.interfaces.nsIObserverService);
  156.     os.addObserver(this, "perm-changed", false);
  157.  
  158.     if (this._type == "install") {
  159.       var enumerator = this._pm.enumerator;
  160.       if (!enumerator.hasMoreElements())
  161.         this._updatePermissions();
  162.     }
  163.  
  164.     this._loadPermissions();
  165.     
  166.     urlField.focus();
  167.   },
  168.   
  169.   uninit: function ()
  170.   {
  171.     var os = Components.classes["@mozilla.org/observer-service;1"]
  172.                        .getService(Components.interfaces.nsIObserverService);
  173.     os.removeObserver(this, "perm-changed");
  174.   },
  175.   
  176.   observe: function (aSubject, aTopic, aData)
  177.   {
  178.     if (aTopic == "perm-changed") {
  179.       var permission = aSubject.QueryInterface(Components.interfaces.nsIPermission);
  180.       if (aData == "added") {
  181.         this._addPermissionToList(permission);
  182.         ++this._view._rowCount;
  183.         this._tree.treeBoxObject.rowCountChanged(this._view.rowCount - 1, 1);        
  184.         // Re-do the sort, since we inserted this new item at the end. 
  185.         gTreeUtils.sort(this._tree, this._view, this._permissions, 
  186.                         this._lastPermissionSortColumn, 
  187.                         this._lastPermissionSortAscending);        
  188.       }
  189.       else if (aData == "changed") {
  190.         for (var i = 0; i < this._permissions.length; ++i) {
  191.           if (this._permissions[i].host == permission.host) {
  192.             this._permissions[i].capability = this._getCapabilityString(permission.capability);
  193.             break;
  194.           }
  195.         }
  196.         // Re-do the sort, if the status changed from Block to Allow
  197.         // or vice versa, since if we're sorted on status, we may no
  198.         // longer be in order. 
  199.         if (this._lastPermissionSortColumn.id == "statusCol") {
  200.           gTreeUtils.sort(this._tree, this._view, this._permissions, 
  201.                           this._lastPermissionSortColumn, 
  202.                           this._lastPermissionSortAscending);
  203.         }
  204.         this._tree.treeBoxObject.invalidate();
  205.       }
  206.       // No UI other than this window causes this method to be sent a "deleted"
  207.       // notification, so we don't need to implement it since Delete is handled
  208.       // directly by the Permission Removal handlers. If that ever changes, those
  209.       // implementations will have to move into here. 
  210.     }
  211.   },
  212.   
  213.   onPermissionSelected: function ()
  214.   {
  215.     var hasSelection = this._tree.view.selection.count > 0;
  216.     var hasRows = this._tree.view.rowCount > 0;
  217.     document.getElementById("removePermission").disabled = !hasRows || !hasSelection;
  218.     document.getElementById("removeAllPermissions").disabled = !hasRows;
  219.   },
  220.   
  221.   onPermissionDeleted: function ()
  222.   {
  223.     if (!this._view.rowCount)
  224.       return;
  225.     var removedPermissions = [];
  226.     gTreeUtils.deleteSelectedItems(this._tree, this._view, this._permissions, removedPermissions);
  227.     for (var i = 0; i < removedPermissions.length; ++i) {
  228.       var p = removedPermissions[i];
  229.       this._pm.remove(p.host, p.type);
  230.     }    
  231.     document.getElementById("removePermission").disabled = !this._permissions.length;
  232.     document.getElementById("removeAllPermissions").disabled = !this._permissions.length;
  233.   },
  234.   
  235.   onAllPermissionsDeleted: function ()
  236.   {
  237.     if (!this._view.rowCount)
  238.       return;
  239.     var removedPermissions = [];
  240.     gTreeUtils.deleteAll(this._tree, this._view, this._permissions, removedPermissions);
  241.     for (var i = 0; i < removedPermissions.length; ++i) {
  242.       var p = removedPermissions[i];
  243.       this._pm.remove(p.host, p.type);
  244.     }    
  245.     document.getElementById("removePermission").disabled = true;
  246.     document.getElementById("removeAllPermissions").disabled = true;
  247.   },
  248.   
  249.   onPermissionKeyPress: function (aEvent)
  250.   {
  251.     if (aEvent.keyCode == 46)
  252.       this.onPermissionDeleted();
  253.   },
  254.   
  255.   _lastPermissionSortColumn: "",
  256.   _lastPermissionSortAscending: false,
  257.   
  258.   onPermissionSort: function (aColumn)
  259.   {
  260.     this._lastPermissionSortAscending = gTreeUtils.sort(this._tree, 
  261.                                                         this._view, 
  262.                                                         this._permissions,
  263.                                                         aColumn, 
  264.                                                         this._lastPermissionSortColumn, 
  265.                                                         this._lastPermissionSortAscending);
  266.     this._lastPermissionSortColumn = aColumn;
  267.   },
  268.   
  269.   _loadPermissions: function ()
  270.   {
  271.     this._tree = document.getElementById("permissionsTree");
  272.     this._permissions = [];
  273.  
  274.     // load permissions into a table
  275.     var count = 0;
  276.     var enumerator = this._pm.enumerator;
  277.     while (enumerator.hasMoreElements()) {
  278.       var nextPermission = enumerator.getNext().QueryInterface(Components.interfaces.nsIPermission);
  279.       this._addPermissionToList(nextPermission);
  280.     }
  281.    
  282.     this._view._rowCount = this._permissions.length;
  283.  
  284.     // sort and display the table
  285.     this._tree.treeBoxObject.view = this._view;
  286.     this.onPermissionSort("rawHost", false);
  287.  
  288.     // disable "remove all" button if there are none
  289.     document.getElementById("removeAllPermissions").disabled = this._permissions.length == 0;
  290.   },
  291.   
  292.   _addPermissionToList: function (aPermission)
  293.   {
  294.     if (aPermission.type == this._type) {
  295.       var host = aPermission.host;
  296.       var capabilityString = this._getCapabilityString(aPermission.capability);
  297.       var p = new Permission(host,
  298.                              (host.charAt(0) == ".") ? host.substring(1,host.length) : host,
  299.                              aPermission.type,
  300.                              capabilityString, 
  301.                              aPermission.capability);
  302.       this._permissions.push(p);
  303.     }  
  304.   },
  305.   
  306.   _updatePermissions: function ()
  307.   {
  308.     try {
  309.       var ioService = Components.classes["@mozilla.org/network/io-service;1"]
  310.                                 .getService(Components.interfaces.nsIIOService);
  311.       var pbi = Components.classes["@mozilla.org/preferences-service;1"]
  312.                           .getService(Components.interfaces.nsIPrefBranch2);
  313.       var prefList = [["xpinstall.whitelist.add", nsIPermissionManager.ALLOW_ACTION],
  314.                       ["xpinstall.whitelist.add.103", nsIPermissionManager.ALLOW_ACTION],
  315.                       ["xpinstall.blacklist.add", nsIPermissionManager.DENY_ACTION]];
  316.  
  317.       for (var i = 0; i < prefList.length; ++i) {
  318.         try {
  319.           // this pref is a comma-delimited list of hosts
  320.           var hosts = pbi.getCharPref(prefList[i][0]);
  321.         } catch(ex) {
  322.           continue;
  323.         }
  324.  
  325.         if (!hosts)
  326.           continue;
  327.  
  328.         hostList = hosts.split(",");
  329.         var capability = prefList[i][1];
  330.         for (var j = 0; j < hostList.length; ++j) {
  331.           // trim leading and trailing spaces
  332.           var host = hostList[j].replace(/^\s*/,"").replace(/\s*$/,"");
  333.           try {
  334.             var uri = ioService.newURI("http://" + host, null, null);
  335.             this._pm.add(uri, this._type, capability);
  336.           } catch(ex) { }
  337.         }
  338.         pbi.setCharPref(prefList[i][0], "");
  339.       }
  340.     } catch(ex) { }
  341.   },
  342.   
  343.   setHost: function (aHost)
  344.   {
  345.     document.getElementById("url").value = aHost;
  346.   }
  347. };
  348.  
  349. function setHost(aHost)
  350. {
  351.   gPermissionManager.setHost(aHost);
  352. }
  353.  
  354. function initWithParams(aParams)
  355. {
  356.   gPermissionManager.init(aParams);
  357. }
  358.  
  359.